<template>
  <div class="common-layout" :class="[themeClass, compactClass]">
    <template v-if="layout === 'top'">
      <el-container>
        <el-header height="54px" style="padding: 0">
          <div class="header">
            <div @click="toMySpace" class="logo">短链接</div>
            <div style="display: flex; align-items: center">
              <el-dropdown>
                <div class="block">
                  <span class="name-span" style="text-decoration: none">{{username}}</span>
                </div>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="toMine">个人信息</el-dropdown-item>
                    <el-dropdown-item divided @click="logout">退出</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </el-header>
        <el-main style="padding: 0">
          <div class="content-box">
            <RouterView class="content-space" />
          </div>
        </el-main>
      </el-container>
    </template>
    <template v-else>
      <el-container>
        <el-aside width="200px" class="sider">
          <div class="sider-logo" @click="toMySpace">短链接</div>
          <el-menu router :default-active="$route.path" class="sider-menu">
            <el-menu-item index="/home/space">我的空间</el-menu-item>
            <el-menu-item index="/home/recycleBin">回收站</el-menu-item>
            <el-menu-item index="/home/account">个人中心</el-menu-item>
          </el-menu>
        </el-aside>
        <el-container>
          <el-header height="54px" style="padding: 0">
            <div class="header">
              <div />
              <div style="display: flex; align-items: center">
                <el-dropdown>
                  <div class="block">
                    <span class="name-span" style="text-decoration: none">{{username}}</span>
                  </div>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item @click="toMine">个人信息</el-dropdown-item>
                      <el-dropdown-item divided @click="logout">退出</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </div>
          </el-header>
          <el-main style="padding: 0">
            <div class="content-box">
              <RouterView class="content-space" />
            </div>
          </el-main>
        </el-container>
      </el-container>
    </template>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { removeKey, removeUsername, getToken, getUsername } from '@/core/auth.js'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'
const { proxy } = getCurrentInstance()
const API = proxy.$API
// 当当前路径和菜单不匹配时，菜单不会被选中
const router = useRouter()
const store = useStore()
const squareUrl = ref('https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png')
const toMine = () => {
  router.push('/home' + '/account')
}
// 登出
const logout = async () => {
  const token = getToken()
  const username = getUsername()
  // 请求登出的接口
  await API.user.logout({ token, username })
  // 删除cookies中的token和username
  removeUsername()
  removeKey()
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  router.push('/login')
  ElMessage.success('成功退出！')
}
// 点击左上方的图片跳转到我的空间
const toMySpace = () => {
  router.push('/home' + '/space')
}
const username = ref('')
onMounted(async () => {
  const actualUsername = getUsername()
  const res = await API.user.queryUserInfo(actualUsername)
  // firstName.value = res?.data?.data?.realName?.split('')[0]
  username.value = truncateText(actualUsername, 8)
})
const extractColorByName = (name) => {
  var temp = []
  temp.push('#')
  for (let index = 0; index < name.length; index++) {
    temp.push(parseInt(name[index].charCodeAt(0), 10).toString(16))
  }
  return temp.slice(0, 5).join('').slice(0, 4)
}

// 辅助函数，用于截断文本
const truncateText = (text, maxLength) => {
  return text.length > maxLength ? text.slice(0, maxLength) + '...' : text
}

const layout = computed(() => store.getters.uiLayout)
const themeClass = computed(() => store.getters.uiTheme === 'dark' ? 'dark' : '')
const compactClass = computed(() => store.getters.uiCompact ? 'compact' : '')
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;

  .el-aside {
    border: 0;
    background-color: #0e5782;

    ul {
      border: 0px;
    }
  }

  .el-main {
    background-color: #e8e8e8;
  }
}

.dark .el-main {
  background-color: var(--el-bg-color);
}

.header {
  color: rgba(0,0,0,.85);
  background-color: #252b30;
  padding: 0 0 0 20px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .block {
    cursor: pointer;
    display: flex;
    align-items: center;
    border: 0px;
  }
}

.content-box {
  height: calc(100vh - 50px);
  background-color: white;
}

.dark .content-box {
  background-color: var(--el-bg-color);
}

:deep(.el-tooltip__trigger:focus-visible) {
  outline: unset;
}

.logo {
  font-size: 15px;
  font-weight: 600;
  color: #e8e8e8;
  font-family: Helvetica, Tahoma, Arial, 'PingFang SC', 'Hiragino Sans GB', 'Heiti SC',
    'Microsoft YaHei', 'WenQuanYi Micro Hei';
  // font-family: 'Helvetica Neue', Helvetica, STHeiTi, Arial, sans-serif;
  cursor: pointer;
}

.logo:hover {
  color: #fff;
}

.link-span {
  color: #fff;
  opacity: .6;
  margin-right: 30px;
  font-size: 16px;
  font-family: 'Helvetica Neue', Helvetica, STHeiTi, Arial, sans-serif;
  cursor: pointer;
  text-decoration: none;
}

.link-span:hover {
  text-decoration: underline !important;
  opacity: 1;
  color: #fff;
}

.name-span {
  color: #fff;
  opacity: .6;
  margin-right: 30px;
  font-size: 12px;
  font-family: 'Helvetica Neue', Helvetica, STHeiTi, Arial, sans-serif;
  cursor: pointer;
  text-decoration: none;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.avatar {
  transform: translateY(-2px);
}

.sider {
  background: #1f2329;
  color: #fff;
  display: flex;
  flex-direction: column;
}
.sider-logo {
  height: 54px;
  display: flex;
  align-items: center;
  padding-left: 16px;
  font-weight: 600;
}
.sider-menu {
  border-right: 0;
}
</style>
